package org.adream.account.rest.api;

import org.adream.account.model.ResultModel;
import org.adream.account.rest.api.model.SiteDomainResp;
import org.adream.account.rest.api.model.SiteInfoResp;
import org.adream.account.rest.api.model.SiteResp;
import org.adream.account.service.DomainProfileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description: 多站点管理接口
 * @author: magic chen
 * @date: 2022/8/5 11:50
 **/
@Controller
public class MultiSiteController {

    private final static Logger logger = LoggerFactory.getLogger(MultiSiteController.class);

    @Autowired
    private DomainProfileService service;


    // 根据访问的域名 获得相对应的自定义信息
    @RequestMapping(value = "/getSiteInfoByDomain", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<Map<String, String>> getInfoByDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String domain = url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
        logger.info("domain:" + domain);
        return service.getInfoByDomain(domain);
    }

    // 站点
    @RequestMapping(value = "/listAllSite", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<List<SiteResp>> listAllSite() {
        return service.listAllSite();
    }

    @RequestMapping(value = "/delSite", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> delSite(@RequestParam("id") String id) {
        return service.deleteSite(id);
    }

    @RequestMapping(value = "/addSite", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> addSite(@RequestParam("siteName") String siteName) {
        return service.addSite(siteName);
    }

    @RequestMapping(value = "/editSiteName", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> editSiteName(@RequestParam("siteId") String siteId, @RequestParam("siteName") String siteName) {
        return service.updateSiteName(siteId, siteName);
    }

    @RequestMapping(value = "/multiSitePage")
    public ModelAndView multiSitePage() {
        ModelAndView mv = new ModelAndView("/multiSite/multiSite");
        return mv;
    }

    @RequestMapping(value = "/multiSiteAddPage")
    public ModelAndView addPage() {
        ModelAndView mv = new ModelAndView("/multiSite/multiSite_add");
        return mv;
    }

    @RequestMapping(value = "/multiSiteEditPage")
    public ModelAndView editPage(@RequestParam("id") String id) {
        ModelAndView mv = new ModelAndView("/multiSite/multiSite_edit");
        ResultModel<SiteResp> resp = service.viewSiteById(id);
        mv.addObject("id", resp.getReturnObject().getId());
        mv.addObject("siteName", resp.getReturnObject().getSiteName());
        return mv;
    }


    // 域名
    @RequestMapping(value = "/siteDomainPage")
    public ModelAndView domainPage(@RequestParam("siteId") String siteId) {
        ModelAndView mv = new ModelAndView("/multiSite/domain");
        ResultModel<List<SiteDomainResp>> resp = service.listAllDomainBySite(siteId);
        mv.addObject("domains", resp.getReturnObject());
        mv.addObject("siteId", siteId);
        return mv;
    }

    @RequestMapping(value = "/delDomain", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> delDomain(@RequestParam("domainId") String domainId) {
        return service.deleteDomain(domainId);
    }

    @RequestMapping(value = "/siteDomainAddPage")
    public ModelAndView domainAddPage(@RequestParam("siteId") String siteId) {
        ModelAndView mv = new ModelAndView("/multiSite/domain_add");
        mv.addObject("siteId", siteId);
        return mv;
    }

    @RequestMapping(value = "/addDomain", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> addDomainForSite(@RequestParam("siteId") String siteId, @RequestParam("domainName") String domainName) {
        return service.addDomainForSite(siteId, domainName);
    }

    @RequestMapping(value = "/siteDomainEditPage")
    public ModelAndView domainEditPage(@RequestParam("domainId") String domainId, @RequestParam("siteId") String siteId) {
        ModelAndView mv = new ModelAndView("/multiSite/domain_edit");
        ResultModel<SiteDomainResp> resp = service.viewDomainById(domainId);
        mv.addObject("domainId", resp.getReturnObject().getId());
        mv.addObject("domain", resp.getReturnObject().getDomain());
        mv.addObject("siteId", siteId);
        return mv;
    }

    @RequestMapping(value = "/updateDomain", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> updateDomainName(@RequestParam("domainId") String domainId, @RequestParam("domainName") String domainName) {
        return service.updateDomainName(domainId, domainName);
    }

    // 站点信息
    @RequestMapping(value = "/siteInfoPage")
    public ModelAndView siteInfo(@RequestParam("siteId") String siteId) {
        ModelAndView mv = new ModelAndView("/multiSite/info");
        ResultModel<List<SiteInfoResp>> resp = service.listAllSiteInfo(siteId);
        mv.addObject("infos", resp.getReturnObject());
        mv.addObject("siteId", siteId);
        return mv;
    }

    @RequestMapping(value = "/delSiteInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> delInfo(@RequestParam("infoId") String infoId) {
        return service.deleteInfo(infoId);
    }

    @RequestMapping(value = "/siteInfoAddPage")
    public ModelAndView infoAddPage(@RequestParam("siteId") String siteId) {
        ModelAndView mv = new ModelAndView("/multiSite/info_add");
        mv.addObject("siteId", siteId);
        return mv;
    }

    @RequestMapping(value = "/addSiteInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> addSiteInfo(@RequestParam("siteId") String siteId,
                                           @RequestParam("dataName") String dataName,
                                           @RequestParam("dataKey") String dataKey,
                                           @RequestParam("dataValue") String dataValue) {
        return service.addInfoForSite(siteId, dataName, dataKey, dataValue);
    }

    @RequestMapping(value = "/siteInfoEditPage")
    public ModelAndView infoEditPage(@RequestParam("infoId") String infoId, @RequestParam("siteId") String siteId) {
        ModelAndView mv = new ModelAndView("/multiSite/info_edit");
        ResultModel<SiteInfoResp> resp = service.viewSiteInfoById(infoId);
        mv.addObject("siteId", siteId);
        mv.addObject("infoData", resp.getReturnObject());
        return mv;
    }

    @RequestMapping(value = "/updateSiteInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultModel<String> updateSiteInfo(@RequestParam("infoId") String infoId,
                                              @RequestParam("dataName") String dataName,
                                              @RequestParam("dataKey") String dataKey,
                                              @RequestParam("dataValue") String dataValue) {
        return service.updateSiteInfo(infoId, dataName, dataKey, dataValue);
    }

}
